In this session, we're going to use Python to post a message to Slack. I set up a team for us so we can mess around with the Slack API.
We're going to use a simple incoming webhook to accomplish this.
API stands for "Application Programming Interface." An API is a way to interact programmatically with a software application.
If you want to post a message to Slack, you could open a browser and navigate to your URL and sign in with your username and password (or open the app), click on the channel you want, and start typing.
OR ... you could post your Slack message with a Python script.
The code for this boot camp is on the public internet. We don't want anyone on the internet to be able to post messages to our Slack channels, so we're going to use an environmental variable to store our webhook.
The environmental variable we're going to use -- IRE_CFJ_2017_SLACK_HOOK
-- should already be stored on your computer.
Python has a standard library module for working with the operating system called os
. The os
module has a data attribute called environ
, a dictionary of environmental variables stored on your computer.
(Here is a new thing: Instead of using brackets to access items in a dictionary, you can use the get()
method. The advantage to doing it this way: If the item you're trying to get doesn't exist in your dictionary, it'll return None
instead of throwing an exception, which is sometimes a desired behavior.)
In [43]:
from os import environ
slack_hook = environ.get('IRE_CFJ_2017_SLACK_HOOK', None)
So far we've been working with tabular data -- CSVs with columns and rows. Most modern web APIs prefer to shake hands with a data structure called JSON (JavaScript Object Notation), which is more like a matryoshka doll.
Python has a standard library module for working with JSON data called json
. Let's import it.
In [37]:
import json
In [38]:
import requests
In [42]:
# build a dictionary of payload data
payload = {
'channel': '#general',
'username': 'IRE Python Bot',
'icon_emoji': ':ire:',
'text': 'helllllllo!'
}
# turn it into a string of JSON
payload_as_json = json.dumps(payload)
In [41]:
# check to see if you have the webhook URL
if slack_hook:
# send it to slack!
requests.post(slack_hook, data=payload_as_json)
else:
# if you don't have the webhook env var, print a message to the terminal
print("You don't have the IRE_CFJ_2017_SLACK_HOOK"
" environmental variable")
Read through the Slack documentation and post a message to a Slack channel ...
Scenario: You cover the Fort Calhoun Nuclear Power Station outside of Omaha, Nebraska. Every day, you'd like to check an NRC website to see if your plant had any "Event Notifications" in the agency's most recent report. You decide to write a Slack script to do this for you. (Ignore, for now, the problem of setting up the script to run daily.)
Breaking down your problem, you need to:
requests
if
statement with in
)requests
to send a message to SlackNotice that we don't need to parse the page with BeautifulSoup -- we're basically just checking for the presence of a string inside a bigger string.
Let's extend the script you just wrote with a function that would allow you to check for the presence of any string on an NRc page for any date of reports -- most days have their own page, though I think weekends are grouped together.
Let's break it down. Inside our function, we need to:
format()
and the date being passed to the functionrequests
status_code
attribute -- 200 means success)
In [ ]: